﻿using IMark.DAO;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMark.UC
{
    public partial class SanPhamLienQuanCtrl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    long ID = long.Parse(Request.QueryString["ID"].ToString());
                    List<SAN_PHAM> ltSanPham = SanPhamModels.LaySanPhamLienQuan(ID);
                    content.InnerHtml = GenerateSPTieuBieu(ltSanPham);
                }
            }
            
        }

        private string GenerateSPTieuBieu(List<SAN_PHAM> ltSanPham)
        {
            StringBuilder strBuilder = new StringBuilder();
            int count = 0;
            for (int j = 0; j < ltSanPham.Count(); j++)
            {
                if (j == 0)
                {
                    strBuilder.Append("<div class='item active'>");
                    strBuilder.Append("<div class='row box-product'>");
                }
                else if (count == 0)
                {
                    strBuilder.Append("<div class='item'>");
                    strBuilder.Append("<div class='row box-product'>");
                }
                count++;

                strBuilder.Append("<div class='col-lg-3 col-md-3 col-sm-6 col-xs-12'>");
                strBuilder.Append("<div class='product-block'><div class='image'>");
                if (ltSanPham[j].NgayBatDau <= DateTime.Now && ltSanPham[j].NgayKetThuc >= DateTime.Now)
                {
                    strBuilder.Append("<div class='product-label product-sale'><span>SALE</span></div>");
                }
                strBuilder.Append("<a class='img' href='ChiTietSP.aspx?ID=" + ltSanPham[j].ID + "'>");
                strBuilder.Append("<img alt='product info' src='images/products/" + ltSanPham[j].HINH_SAN_PHAM.First().Hinh + "' title='product title'/></a></div>");
                strBuilder.Append("<div class='product-meta'>");
                strBuilder.Append("<div class='name'><a href='Products.aspx'>" + ltSanPham[j].TenSanPham + "</a></div>");
                if (ltSanPham[j].NgayBatDau <= DateTime.Now && ltSanPham[j].NgayKetThuc >= DateTime.Now)
                {
                    strBuilder.Append("<div class='big-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", (ltSanPham[j].DonGia - ((ltSanPham[j].DonGia * ltSanPham[j].GiamGia) / 100))) + "<span class='sym'>đ</span></span> <span class='price-old'><span class='sym'></span></span> </div>");
                }
                else
                {
                    strBuilder.Append("<div class='big-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'>đ</span></span> <span class='price-old'><span class='sym'></span></span> </div>");
                }
                strBuilder.Append("<div class='big-btns'>");
                strBuilder.Append("<a class='btn btn-default btn-view pull-left' href='ChiTietSP.aspx?ID=" + ltSanPham[j].ID + "'>Xem</a> <button class='btn btn-default btn-addtocart pull-right'  onclick='ThemVaoGioHang(" + ltSanPham[j].ID + ")'>Thêm</button>");
                strBuilder.Append("</div>");
                if (ltSanPham[j].NgayBatDau <= DateTime.Now && ltSanPham[j].NgayKetThuc >= DateTime.Now)
                {
                    strBuilder.Append("<div class='small-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", (ltSanPham[j].DonGia - ((ltSanPham[j].DonGia * ltSanPham[j].GiamGia) / 100))) + "<span class='sym'>đ</span></span>");
                    strBuilder.Append(" <span class='price-old'>" + String.Format(CultureInfo.InvariantCulture, "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'></span></span> </div>");
                }
                else
                {
                    strBuilder.Append("<div class='small-price'><span class='price-new'>" + String.Format(CultureInfo.InvariantCulture,
                                "{0:0,0}", ltSanPham[j].DonGia) + "<span class='sym'>đ</span></span>  </div>");
                }

                strBuilder.Append(GenerateRation(ltSanPham[j].DANH_GIA.ToList()));
                strBuilder.Append("<div class='small-btns'>");
                strBuilder.Append("<button class='btn btn-default btn-compare pull-left' data-toggle='tooltip' title='Add to Compare' onclick='Compare(" + ltSanPham[j].ID + ")'> <i class='fa fa-retweet fa-fw'></i> </button>");
                strBuilder.Append("<button class='btn btn-default btn-wishlist pull-left' data-toggle='tooltip' title='Add to Wishlist' onclick='ThemVaoYeuThich(" + ltSanPham[j].ID + ")'> <i class='fa fa-heart fa-fw'></i> </button>");
                strBuilder.Append("<button class='btn btn-default btn-addtocart pull-left' onclick='AddToCart(" + ltSanPham[j].ID + ")' data-toggle='tooltip' title='Add to Cart'><i class='fa fa-shopping-cart fa-fw'></i></button>");
                strBuilder.Append("</div>");
                strBuilder.Append("</div>");
                strBuilder.Append("<div class='meta-back'></div>");
                strBuilder.Append("</div>");
                strBuilder.Append("</div>");
                if (count == 4)
                {
                    strBuilder.Append("</div>");
                    strBuilder.Append("</div>");
                    count = 0;
                }
            }
            return strBuilder.ToString();
        }

        public string GenerateRation(List<DANH_GIA> GG)
        {
            decimal tongsodanhgia = GG.Where(w => w.SoSao != 0).Count();
            decimal tongsosao = GG.Where(w => w.SoSao != 0).Sum(s => s.SoSao);
            decimal sao = 0;
            if (tongsodanhgia > 0 & tongsosao > 0)
                sao = Math.Round((tongsosao / tongsodanhgia));
            return GenerateRatingTheoSoSao(int.Parse(sao.ToString()));
        }

        public string GenerateRatingTheoSoSao(int sao)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<div class='rating'>");
            for (int i = 0; i < sao; i++)
            {
                str.Append("<i class='fa fa-star'></i>");
            }
            if (5 - sao > 0)
            {
                for (int i = 0; i < (5 - sao); i++)
                {
                    str.Append("<i class='fa fa-star-o'></i>");
                }
            }
            str.Append("</div>");
            return str.ToString();
        }
    }
}